package com.lovo;

import java.io.*;
import java.nio.Buffer;
import java.util.Scanner;

public class Test01 {

    public void testInputStream(){
        FileInputStream fis = null;
        try{
            // 1.创建字节流对象
            fis = new FileInputStream("d:/a.txt");
            int d = 0;
            // 2.循环读取数据
//            while((d = fis.read()) != -1){
//                System.out.print((char)d);
//            }
            byte [] bs = new byte[1024];
            while((d = fis.read(bs)) != -1){
                System.out.print(new String(bs));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            if(fis != null){
                try {
                    // 3.关闭流
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void testReader(){
        FileReader fr = null;
        BufferedReader br = null;
        try {
            // 流连接
            fr = new FileReader("d:/a.txt");
            br = new BufferedReader(fr);
//            int d = 0;
//            while((d = fr.read()) != -1){
//                System.out.print((char)d);
//            }
            String str = null;
            while((str = br.readLine()) != null){
                System.out.println(str);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fr != null){
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void testOutputStream(){
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("d:/b.txt");
            String str = "hello 字节流！";
            fos.write(str.getBytes());
//            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public void testWrite(){
        FileWriter fw = null;
        try {
            fw = new FileWriter("d:/a/c.txt");
            fw.write("你好啊，字符流");
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            if(fw != null){
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }


    public static void main(String[] args) {
        Test01 test01 = new Test01();
//        test01.testInputStream();
//        test01.testReader();
//        test01.testOutputStream();
//        test01.testWrite();
//        test01.copy("D:\\Pictures\\vscode.png","D:\\Pictures\\vscode_bak.png");
        test01.append("d:/a.txt");


    }
    // 1. 写一个方法，实现文件的拷贝
    public void copy(String src,String dist){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(src);
            fos = new FileOutputStream(dist);
            int d = 0;
//            while((d = fis.read()) != -1){
//                fos.write(d);
//            }
            byte [] bs = new byte[1024];
            while((d = fis.read(bs)) != -1){
                fos.write(bs,0,d);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 2. 输入一段字符串，将该字符串追加到指定文本文件中
    public void append(String path){
        FileWriter fw = null;
        Scanner sc = new Scanner(System.in);
        try {
            fw = new FileWriter(path,true);
            System.out.println("请输入追加的内容：");
            String str = sc.next();
//            fw.write(str);
            fw.append(str);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fw != null){
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}
